﻿using System;
using System.Collections;
using System.Linq;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Reports.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Reports.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SystemOverallReport : BasePage<SystemOverallReportViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.GetReport += new EventHandler<EventArgs>(Master_GetReport);
            if (!IsPostBack && !Ext.IsAjaxRequest)
            {
                FillReportParams();
            }
        }

        /// <summary>
        /// Handles the GetReport event of the Master control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void Master_GetReport(object sender, EventArgs e)
        {
            SystemOverallParameters SysParam = new SystemOverallParameters();

            SysParam.committes = cmbCommittee.SelectedItems.ToList().Select(v => v.Value.ToGuid()).ToList();
            SysParam.educationallevel = cmbEducationalLevel.SelectedItem.Value.ToGuid();
            SysParam.loanplacetype = cmbLoanPlaceType.SelectedItem.Value.ToGuid();
            SysParam.loantype = cmbLoanType.SelectedItem.Value.ToGuid();
            SysParam.project = cmbProject.SelectedItems.ToList().Select(v => v.Value.ToGuid()).ToList();
            SysParam.projectstage = cmbProjectStage.SelectedItem.Value.ToGuid();
            SysParam.responsible = cmbResponsibles.SelectedItem.Value.ToGuid();
            SysParam.socialstatus = cmbSocialStatus.SelectedItem.Value.ToGuid();

            SysParam.agefrom = txtAgeFrom.Text.ToInt32();
            SysParam.ageto = txtAgeTo.Text.ToInt32();
            SysParam.averageincomefrom = txtAverageSalaryFrom.ToInt32();
            SysParam.averageincometo = txtAverageSalaryTo.Text.ToInt32();
            SysParam.familynumberfrom = txtFamilyNumberFrom.Text.ToInt32();
            SysParam.familynumberto = txtFamilyNumberto.Text.ToInt32();
            SysParam.loanpaymentperiod = txtLoanPaymentPeriod.Text.ToInt32();
            SysParam.boardnumber = txtBoardNumber.Text.ToInt32();

            SysParam.job = txtjob.Text;
            SysParam.reasonofrefuse = txtReasonOfRefuse.Text;

            SysParam.loanexecutioamount = Convert.ToDouble(txtLoanExecutionAmount.Text);

            SysParam.boarddatefrom = dtBoardDateFrom.SelectedDate;
            SysParam.boarddateto = dtBoardDateTo.SelectedDate;
            SysParam.loanexecutiondatefrom = dtExecutionDateFrom.SelectedDate;
            SysParam.loanexecutiondateto = dtExecutionDateTo.SelectedDate;
            SysParam.repaymentterminationdatefrom = dtRepaymentTerminationDateFrom.SelectedDate;
            SysParam.repaymentterminationdateto = dtRepaymentTerminationDateTo.SelectedDate;
            SysParam.requestdatefrom = dtRequestDateFrom.SelectedDate;
            SysParam.requestdateto = dtRequestDateTo.SelectedDate;


            IEnumerable result = this.ViewModel.SystemOverall(SysParam);

            gvResult.DataSource = result;
            gvResult.DataBind();
        }

        /// <summary>
        /// Fills the report params.
        /// </summary>
        private void FillReportParams()
        {
            strCommittee.DataSource = this.ViewModel.GetAllCommittee();
            strCommittee.DataBind();

            strEducationalLevel.DataSource = this.ViewModel.GetAllEducationalLevels();
            strEducationalLevel.DataBind();

            strLoanPlaceTypes.DataSource = this.ViewModel.GetAllLoanPlaces();
            strLoanPlaceTypes.DataBind();

            strLoanType.DataSource = this.ViewModel.GetAllLoanTypes();
            strLoanType.DataBind();

            strProjects.DataSource = this.ViewModel.GetAllProjects();
            strProjects.DataBind();

            strProjectStage.DataSource = this.ViewModel.GetAllProjectStage();
            strProjectStage.DataBind();

            strResponsibles.DataSource = this.ViewModel.GetAllResponsibles();
            strResponsibles.DataBind();

            strSocialStatus.DataSource = this.ViewModel.GetAllEducationalLevels();
            strSocialStatus.DataBind();


        }
    }
}
